package com.ymatou.iapi.productmanager.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ymttest.business.service.ProductManagerCallServiceV3;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.parameter.GetNewProductDescriptionInfoBean;
import com.ymatou.iapi.productmanager.parameter.ProdTplBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.parameter.product.CatalogPropertySaveModel;
import com.ymatou.iapi.productmanager.service.GetNewProductDescriptionInfoCall;
import com.ymatou.iapi.productmanager.service.GetProductAndCatalogInfoCall;
import com.ymatou.iapi.productmanager.service.SaveProductTemplateCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.SellerProductCallServiceV2;
import com.ymttest.database.model.Ymt_ProductDescConfigPicture;
import com.ymttest.database.model.Ymt_ProductDescConfigWord;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.Ymt_ProductDescConfigPictureWapper;
import com.ymttest.database.sqlwapper.Ymt_ProductDescConfigWordWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;

public class Ts_GetNewProductDescriptionInfo {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static GetNewProductDescriptionInfoCall call = new GetNewProductDescriptionInfoCall();
	private static GetNewProductDescriptionInfoBean bean = new GetNewProductDescriptionInfoBean();
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("Get product detail");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetNewProductDescriptionInfo_001() {
		Logger.start(true, "Get new product detail");
		try{
			SaveProductTemplateCall saveProductTemplateCall = new SaveProductTemplateCall();
			saveProductTemplateCall.deleteTemplate(sellerId);
			//	模版类型（1，尺码模版 2，买家须知 3，买手服务）
			ProdTplBean sizeTemplate = saveProductTemplateCall.saveTemplate(sellerId, 1);
			ProdTplBean buyerNoticeTemplate = saveProductTemplateCall.saveTemplate(sellerId, 2);
			ProdTplBean sellerServiceTemplate = saveProductTemplateCall.saveTemplate(sellerId, 3);
			
			//get property 
			Map<String, Object> thirdCategoryAndPropertyVal = SellerProductCallServiceV2.getThirdCategoryAndPropertyValue();
			
			//发布商品
			//JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl.json");
			//JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.productmanager/AddSellerProductCall/addsellerproductcalltpl.json");
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			addProdReq.setSellerId(sellerId);
			addProdReq.setSellerName(sellerName);
			String ThirdCategoryId = thirdCategoryAndPropertyVal.get("ThirdCategory").toString();
			addProdReq.getProduct().setThirdCategoryId(Integer.valueOf(ThirdCategoryId));
			addProdReq.getProduct().getCatalogList().add(ProductManagerCallServiceV2.initCatalogSaveModel(10d,10,"颜色","颜色"+1));
			CatalogPropertySaveModel catalogPropertySaveModel = new CatalogPropertySaveModel();
			CatalogPropertySaveModel catalogPropertySaveModel1 = new CatalogPropertySaveModel();
//			catalogPropertySaveModel.setPropertyId("71");
			catalogPropertySaveModel.setPropertyName("颜色");
			catalogPropertySaveModel.setPropertyValue("PropertyValue1");
//			catalogPropertySaveModel1.setPropertyId("60");
			catalogPropertySaveModel1.setPropertyName("颜色");
			catalogPropertySaveModel1.setPropertyValue("PropertyValue2");
			List<CatalogPropertySaveModel> aList = new ArrayList<CatalogPropertySaveModel>();
			aList.add(catalogPropertySaveModel1);
			aList.add(catalogPropertySaveModel);
			addProdReq.getProduct().getCatalogList().get(0).setPropertyList(aList);
			addProdReq.getProduct().getDescModel().setSizeTemplateId(sizeTemplate.getTemplateId());
			addProdReq.getProduct().getDescModel().setBuyerNoticeId(buyerNoticeTemplate.getTemplateId());
			addProdReq.getProduct().getDescModel().setSellerServiceId(sellerServiceTemplate.getTemplateId());
			
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			
			//属性检查
			//SellerProductCallServiceV2.checkProdProperty(productId,(JSONArray)thirdCategoryAndPropertyVal.get("PropertyList"));
			
			
		     
			bean.setuserId(sellerId);
			bean.setproductId(productId);
			call.setData(bean);
			call.callService();
			Ymt_ProductDescConfigPictureWapper pic=new Ymt_ProductDescConfigPictureWapper();
			List<Ymt_ProductDescConfigPicture> pics=pic.selectbyProductId(productId);
			Ymt_ProductDescConfigWord word=new YmtProductsIWapper().getProductDescConfigWord(productId);
			
			Logger.verifyEquals(200,new JSONObject(call.getReturnData()).get("Code"),"验证code");
			Logger.verifyEquals("操作成功",new JSONObject(call.getReturnData()).get("Msg"),"验证Msg");
			JSONObject productInfo = new JSONObject(call.getReturnData()).getJSONObject("Data").getJSONObject("ProductDescriptionInfo");
			Logger.verifyEquals(word.getWordContent(),productInfo.getString("WordContent"),"验证数据表Ymt_ProductDescConfigWord中的WordContent");
			
			for(int i=0;i<pics.size();i++)
			{
				int count=i+1;
				Logger.verifyEquals(pics.get(i).getPictureUrl(),new JSONObject(new JSONArray(productInfo.getString("Pictures")).get(i).toString()).get("PictureUrl"),"验证集合第"+count+"个值，数据表Ymt_ProductDescConfigPicture中的PictureUrl");
				Logger.verifyEquals(pics.get(i).getProductDescConfigPictureID(),new JSONObject(new JSONArray(productInfo.getString("Pictures")).get(i).toString()).get("Id"),"验证集合第"+count+"个值，数据表Ymt_ProductDescConfigPicture中的Id");
				Logger.verifyEquals(pics.get(i).getShopPhotoID(),new JSONObject(new JSONArray(productInfo.getString("Pictures")).get(i).toString()).get("ShopPhotoId"),"验证集合第"+count+"个值，数据表Ymt_ProductDescConfigPicture中的ShopPhotoId");
				Logger.verifyEquals(pics.get(i).getPictureWidth(),new JSONObject(new JSONArray(productInfo.getString("Pictures")).get(i).toString()).get("PictureWidth"),"验证集合第"+count+"个值，数据表Ymt_ProductDescConfigPicture中的PictureWidth");
				Logger.verifyEquals(pics.get(i).getPictureHeight(),new JSONObject(new JSONArray(productInfo.getString("Pictures")).get(i).toString()).get("PictureHeight"),"验证集合第"+count+"个值，数据表Ymt_ProductDescConfigPicture中的PictureHeight");
				Logger.verifyEquals(pics.get(i).getSorting(),new JSONObject(new JSONArray(productInfo.getString("Pictures")).get(i).toString()).get("Sorting"),"验证集合第"+count+"个值，数据表Ymt_ProductDescConfigPicture中的Sorting");
			}
		
			//模板检查
			//check product template
			/*List<ProdTplBean> prodTplBeanList = Arrays.asList(sizeTemplate,buyerNoticeTemplate,sellerServiceTemplate);
			SellerProductCallServiceV2.checkTemplateWithProd(productId,prodTplBeanList);*/
			/**
			 * AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			
			for(int i=0;i<6;i++){
				addProdReq.getProduct().getCatalogList().add(ProductManagerCallServiceV2.initCatalogSaveModel(10d,10,"颜色","颜色"+i));
			}
			List<String> skuList = generateSKUList(addProdReq.getProduct().getCatalogList().size());
			for (int i=0;i<addProdReq.getProduct().getCatalogList().size();i++){
				String sku=skuList.get(i);
				addProdReq.getProduct().getCatalogList().get(i).setSku(sku);
			}
			
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			 */
			/*Map<String, Object> updateMap = MapUtil.hashMap();
			updateMap.put("sellerId", sellerId);
			updateMap.put("sellerName", sellerName);
			updateMap.put("product->thirdCategoryId", thirdCategoryAndPropertyVal.get("ThirdCategory"));
			updateMap.put("product->propertyList", (JSONArray)thirdCategoryAndPropertyVal.get("PropertyList"));
			updateMap.put("product->descModel->sizeTemplateId", sizeTemplate.getTemplateId());
			updateMap.put("product->descModel->buyerNoticeId",buyerNoticeTemplate.getTemplateId());
			updateMap.put("product->descModel->sellerServiceId", sellerServiceTemplate.getTemplateId());	
			JsonBeanHelper.updateJsonBean(addProductBean, updateMap);*/
			
			//String productId = ProductManagerCallServiceV2.addNewProduct(addProductBean).getJSONObject("Data").getString("ProductId");
			// select top 100* from Ymt_ProductDescConfigPicture where ProductId= 'af0c3f43-d98a-4593-93ba-e5c92f2d0107'
			
			//JSONObject ret = call.callAndGetReturnData(sellerId, productId);
			
//			JSONObject productInfo = new JSONObject(call.getReturnData()).getJSONObject("Data").getJSONObject("ProductDescriptionInfo");
//			
//			Logger.verifyEquals(productInfo.getJSONObject("SizeTemplate").getInt("SizeTemplateId"), sizeTemplate.getTemplateId(), "商品信息 返回校验 sizeTemplate");
//			Logger.verifyEquals(productInfo.getJSONObject("SizeTemplate").getString("Name"), sizeTemplate.getTemplateName(), "商品信息 返回校验 sizeTemplate");
//			Logger.verifyEquals(productInfo.getJSONObject("BuyerNotice").getInt("BuyerNoticeId"), buyerNoticeTemplate.getTemplateId(), "商品信息 返回校验 BuyerNotice");
//			Logger.verifyEquals(productInfo.getJSONObject("BuyerNotice").getString("Name"), buyerNoticeTemplate.getTemplateName(), "商品信息 返回校验 BuyerNotice");
//			Logger.verifyEquals(productInfo.getJSONObject("SellerService").getInt("SellerServiceId"), sellerServiceTemplate.getTemplateId(), "商品信息 返回校验 SellerService");
//			Logger.verifyEquals(productInfo.getJSONObject("SellerService").getString("Name"), sellerServiceTemplate.getTemplateName(), "商品信息 返回校验 SellerService");
//			
//			
//			
//			
//			SellerProductCallServiceV2.checkPropertyList( productInfo.getJSONArray("PropertyList"), Integer.parseInt(thirdCategoryAndPropertyVal.get("ThirdCategory").toString()), (JSONArray)thirdCategoryAndPropertyVal.get("PropertyList"));
			
			
			
		/*	List<SpuProperty>  spuPropertyList = new SpuPropertyWapper().selectByThirdcategoryidId(Integer.parseInt(thirdCategoryAndPropertyVal.get("ThirdCategory").toString()));
			JSONArray propertyList = productInfo.getJSONArray("PropertyList");
			Logger.verifyEquals(spuPropertyList.size(), propertyList.length(), "商品信息 返回校验 PropertyList record");
			
			for(int i=0;i<propertyList.length();i++){
				JSONObject propertyJson = propertyList.getJSONObject(i);
				Integer PropertyId = propertyJson.getInt("PropertyId");
				SpuProperty spuProperty = spuPropertyList.stream().filter(ele->ele.getPropertyId().equals(PropertyId)).findFirst().orElse(null);
				
				Logger.verifyNotNull(spuProperty, "spuProperty propertyid:"+PropertyId+" should be exists.");
				
				Map<String, Object> tgtMap = new HashMap<>();
				tgtMap.put("PropertyName", spuProperty.getName());
				tgtMap.put("PropertyValueType", spuProperty.getStyles());
				tgtMap.put("IsMultiple", spuProperty.getMultiSelect());
				
				//to do values check 
				propertyJson.remove("Values");
				AssertService.assertResultEqual(tgtMap, MapUtil.jsonToMap(propertyJson),"PropertyList id:"+ PropertyId+"check ");
		
				
				//property value check if has 
				JSONArray propertys = (JSONArray)thirdCategoryAndPropertyVal.get("PropertyList");
				boolean isHasVal = false;
				for(int j=0;j<propertys.length();j++){
					if (propertys.getJSONObject(j).getInt("PropertyId")==PropertyId){
						AssertService.checkArray((propertys.getJSONObject(j).getJSONArray("PropertyValue")),propertyJson.getJSONArray("InputValues"));
						isHasVal = true;
						break;
					}
				}
				
				
				if (!isHasVal){
					Logger.verifyEquals(0, propertyJson.getJSONArray("InputValues").length(), "InputValues should be empty");
				}
			}*/
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
}
